<template>
  <div id="history" v-loading="loading" element-loading-text="加载数据中...">
    <el-table
      :data="history"
      border
      :height="height"
      v-show="$route.name==='history'"
      style="width: 100%">
      <el-table-column
        prop="COURSE_NAME"
        label="科目">
      </el-table-column>
      <el-table-column
        prop="TK_NAME"
        label="题库">
      </el-table-column>
      <el-table-column
        label="得分"
        width="80">
        <template v-slot:default="{row:{H_SCORE}}">
          <el-tag style="width: 50px;text-align: center" effect="dark" type="warning">
            {{H_SCORE}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="CREATE_TIME"
        label="日期">
      </el-table-column>
      <el-table-column
        label="耗时"
        width="120">
        <template v-slot:default="{row:{TIME}}">
          <el-tag effect="dark">
            {{formatTime(TIME).minute}} : {{formatTime(TIME).second}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="80">
        <template slot-scope="{row}">
          <el-button @click="getTopicInfo(row)" type="text" size="small">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <router-view></router-view>
  </div>
</template>

<script>
  import {mapActions, mapState} from "vuex";
  import {formatTime} from "@/util/function"

  export default {
    name: "history",
    data() {
      return {
        loading: false,
        height: null
      }
    },
    created() {
      this.loading = true
      this.getHistory().finally(() => this.loading = false)
    },
    mounted() {
      this.height = document.querySelector('#history').clientHeight
    },
    computed: {
      ...mapState({
        history: state => state.history
      })
    },
    methods: {
      ...mapActions(['getHistory', 'getTopicHistory']),
      formatTime,
      getTopicInfo(history) {
        this.loading = true
        this.getTopicHistory(history).then(() => {
          this.$router.push({name: 'historyGrade'})
        }).catch(() => {
          this.$notify.error({
            title: 'ERROR',
            message: '获取数据失败!'
          })
        }).finally(() => this.loading = false)
      }
    },
  }
</script>

<style scoped>
  #history {
    height: calc(100% - 20px);
    margin-bottom: 20px;
  }
</style>
